<template>
  <van-button
    v-if="value"
    type="info"
    color="#3296fa"
    round
    size="small"
    icon="plus"
    :loading="loading"
    @click="onFollowed"
    >关注</van-button
  >
  <van-button
    v-else
    round
    size="small"
    :loading="loading"
    @click="onFollowed"
    >已关注</van-button
  >
</template>

<script>
// 关注用户与取消关注用户
import { addFollow, deleteFollow } from '@/api/user'
export default {
  name: 'FollowUser',
  components: {},
  props: {
    value: {
      type: Boolean,
      required: true
    },
    articleAutId: {
      type: [Number, String, Object],
      required: true
    }
  },
  data () {
    return {
      loading: false
    }
  },
  created () {},
  mounted () {},
  methods: {
    // 关注与取消关注
    async onFollowed () {
      this.loading = true
      try {
        // 判断是关注用户还是取消关注
        if (this.value) {
          // 已关注，取消关注
          const { data } = await deleteFollow(this.articleAutId)
          console.log(data)
        } else {
          // 未关注，关注
          const { data } = await addFollow(this.articleAutId)
          console.log(data)
        }
        // 更新视图
        this.$emit('input', !this.value)
      } catch (err) {
        this.$toast.fail('服务器错误请稍后再试')
      }
      this.loading = false
    }
  }
}
</script>

<style scoped lang="less"></style>
